﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Stock.Core
{
    public static class WebBrowserElement
    {
        public static readonly DependencyProperty BodyProperty =
        DependencyProperty.RegisterAttached("Url", typeof(string), typeof(WebBrowserElement), new PropertyMetadata(OnUrlChanged));

        public static string GetUrl(DependencyObject dependencyObject)
        {
            return (string)dependencyObject.GetValue(BodyProperty);
        }

        public static void SetUrl(DependencyObject dependencyObject, string body)
        {
            dependencyObject.SetValue(BodyProperty, body);
        }

        private static void OnUrlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is WebBrowser webBrowser)
            {
                string url = e.NewValue as string;
                if (!string.IsNullOrEmpty(url))
                {
                    webBrowser.Navigate(url);
                }
            }
        }
    }
}
